define([], function() {
    let MVC = (function() {
        // M
        let Module = (function() {
            let _module = {}
            return {
                add(name, obj) {
                    let arr = name.split('.');
                    let tail = arr.pop();
                    // console.log(arr);
                    let cache = _module;
                    arr.forEach(item => {
                        if (cache[item] === undefined) {
                            cache[item] = {};
                            // console.log(111);
                        };
                        if (cache[item] != null && typeof cache[item] === 'object') {
                            cache = cache[item];
                            // console.log(22);
                        } else {
                            throw new Error('不能在值类型上添加数据')
                        }
                    })
                    cache[tail] = obj;
                },
                // 获取数据
                get(name) {
                    let arr = name.split('.');
                    let cache = _module;
                    // arr.forEach(item => {
                    //     cache = cache[item];
                    //     console.log(cache);
                    //     return cache;
                    // });
                    return arr.reduce((pre, item) => pre[item], cache);
                }
            }
        })();
        // V
        let View = (function() {
            let _view = {}
            return {
                // 存储数据
                add(name, fn) {
                    _view[name] = fn;
                },

                // 执行函数
                render(name) {
                    return _view[name] && _view[name](Module);
                }
            }
        })();
        // C
        let Ctrl = (function() {
            let _ctrl = {}
            return {
                // 存储数据
                add(name, fn) {
                    _ctrl[name] = fn;
                },
                // 初始化方法
                init() {
                    for (let k in _ctrl) {
                        _ctrl[k] && _ctrl[k](Module, View)
                    }
                }
            }
        })();

        let Obsever = (function() {
            var obj = {}
            return {
                on: function(key, fn) {
                    if (obj[key]) {
                        obj[key].push(fn)
                    } else {
                        obj[key] = [fn]
                    }
                },
                tirgger: function(key, ...args) {
                    // 获取参数
                    if (obj[key]) {
                        obj[key] && obj[key].forEach(fn => fn(...args))
                    }
                }
            }
        })();

        return {
            addModule(name, obj) {
                Module.add(name, obj);
            },
            addView(name, fn) {
                View.add(name, fn);
            },
            addCtrl(name, fn) {
                Ctrl.add(name, fn);
            },
            install() {
                Ctrl.init();
            },
            ...Obsever
        }
    })();
    return MVC;
})